package cn.walls1717.service.impl;

import cn.walls1717.domain.Permission;
import cn.walls1717.domain.Role;
import cn.walls1717.mapper.RoleMapper;
import cn.walls1717.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author CJ
 * @date 2020/8/12
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class RoleServiceImpl implements RoleService {

    private RoleMapper roleMapper;

    @Autowired
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }

    @Override
    public List<Role> findAll() throws Exception {
        return roleMapper.findAll();
    }

    @Override
    public void save(Role role) throws Exception {
        roleMapper.save(role);
    }

    @Override
    public Role findById(String roleId) throws Exception {
        return roleMapper.findById(roleId);
    }

    @Override
    public List<Permission> findOtherPermission(String roleId) throws Exception {
        return roleMapper.findOtherPermission(roleId);
    }

    @Override
    public void addPermissionToRole(String roleId, String[] permissionIds) throws Exception {
        for(String permissionId : permissionIds) {
            roleMapper.addPermissionToRole(roleId, permissionId);
        }
    }
}
